package sw.EngAdroid;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;

import netcode.helper.NetcodeHelper;

//import netcode.server.NetworkGame;

public class Lobby {
	private Map<Integer,Game> allGames;
	
	public Lobby(){
		allGames = new ConcurrentSkipListMap<Integer,Game>();
	}
	
	public void insertGame(int gameID, String gameName, int gameState){
		allGames.put(gameID, new Game(gameName, 0, gameState));
	}
	
	// Now unnecessary.
//	public void insertGame(int gameID, String gameName, ArrayList<Player> p){
//		allGames.put(gameID, new Game(gameName, 7));
//		for(int i = 0; i < p.size(); i++){
//			allGames.get(gameID).addPlayer(p.get(i));
//		}
//	}
	
	public void deleteGame(int gameKey){
		allGames.remove(gameKey);
	}
	
	public void updatePlayersLobby(int gameID, ArrayList<Player> p){
//		for(int i = 0; i < p.size(); i++){
//			// Add players to game given id if not already in game
//			if(allGames.get(gameID).getPlayers().contains(p.get(i))){
//				allGames.get(gameID).addPlayer(p.get(i));
//			}
//		}
		allGames.get(gameID).setPlayers(p);
		
	}
	
	public String toString(){
		String output = "******Server Lobby******\n";
		Iterator<Integer> gameIterator = allGames.keySet().iterator();
		while(gameIterator.hasNext()){
			int gameID = gameIterator.next();
			output += gameID +  "\t" + allGames.get(gameID).getName();
			if(allGames.get(gameID).getGameState() == NetcodeHelper.WAITING_TO_START){
				output += '\t' + "Waiting for players";
			}else if(allGames.get(gameID).getGameState()== NetcodeHelper.STARTED){
				output += '\t' + "Started";
			}
			output += '\n';
		}
		return output;
	}
	
	public Map<Integer, Game> getAllGames(){
		return allGames;
	}
	
}
